﻿using Common.XmlHandle;
using Network.UpdateData;
using SFJDService.TransportHandle;
using System;
using System.Net.Sockets;

namespace SFJDService.SocketHandle
{
    /// <summary>
    /// <para>发出消息：TransGuid♠IPReceive♠NameReceive♠IPSend♠NameSend</para>
    /// </summary>
    class PH05SeStratSend : PacketHandle
    {
        public PH05SeStratSend(Socket socket, Packet packet) : base(socket, packet)
        {
        }

        protected void Handle2()
        {
            string[] buffer = Message.Split('♠');
            Guid guidTrans = new Guid(buffer[0]);

            if (Runtime.xmlUpload.isThereGuid(guidTrans))
            {
                FileSend.tasks.Add(new ClassTransCSInfo(guidTrans, GetIpSend(), buffer[1], Runtime.myIP, Runtime.thisUser.UserName));

                int port = GetAvailablePort.GetFirstAvailablePort();//获取回传端口号

                string[] buf = buffer[2].Split('♥');
                int[] ports = new int[buf.Length];
                for(int i = 0; i < buf.Length; i++)
                {
                    ports[i] = Int32.Parse(buf[i]);
                }

                try
                {
                    Launcher.SendTcp(typeof(PH05ReStartReceive).GetDictianaryKey(), GetIpSend(),
                        buffer[0] + '♠' + GetIpSend() + '♠' + buffer[1] + '♠' + Runtime.myIP + '♠' + Runtime.thisUser.UserName + '♠' + port + '♠' + buffer[2]);
                    Delay(1000);
                    new TransportSend(guidTrans, GetIpSend(), ports, port);
                }
                catch (Exception e)
                {
                    throw new Exception(GetType() + "   " + e.ToString());
                }
            }
            else
            {
                throw new Exception();
            }
        }

        protected override void Handle()
        {
            string[] buffer = Message.Split('♠');
            if (Runtime.xmlUpload.isThereGuid(new Guid(buffer[0])))
            {
                FileSend.tasks.Add(new ClassTransCSInfo(new Guid(buffer[0]), GetIpSend(), buffer[1], Runtime.myIP, Runtime.thisUser.UserName));
                try
                {
                    Launcher.SendTcp(typeof(PH05ReStartReceive).GetDictianaryKey(), GetIpSend(), 
                        buffer[0] + '♠' + GetIpSend() + '♠' + buffer[1] + '♠' + Runtime.myIP + '♠' + Runtime.thisUser.UserName);
                    Delay(500);
                    new FileSend();
                }
                catch (Exception e)
                {
                    throw new Exception(GetType() + "   " + e.ToString());
                    //Launcher.SendTcp(typeof(PH05ReStartReceive).GetDictianaryKey(), GetIpSend(),
                    //    buffer[0] + '♠' + GetIpSend() + '♠' + buffer[1] + '♠' + Runtime.myIP + '♠' + Runtime.thisUser.UserName);
                    //for (int i = 0; i < 20; i++)
                    //{
                    //    try
                    //    {
                    //        Delay(500);
                    //        new FileSend();
                    //        break;
                    //    }
                    //    catch
                    //    {
                    //        Delay(100 * i);
                    //    }
                    //}
                }
            }
            else
            {
                throw new Exception();
            }
        }
    }
}
